package com.hxx.h.note

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.InputFilter
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextUtils
import android.text.style.BulletSpan
import android.text.style.ParagraphStyle
import android.util.Log
import android.view.View
import androidx.core.text.getSpans
import androidx.core.text.toHtml
import com.hxx.h.R
import com.hxx.widget.bar.BarAppCometActivity
import kotlinx.android.synthetic.main.activity_note_editor.*

class NoteEditorActivity : BarAppCometActivity() {

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_note_editor)
        export.setOnClickListener {
            Log.i(TAG, "onCreate: ${test.text?.toHtml()}")
            test.text?.apply {
                getSpans<Any>(0, length).forEach {
                    Log.i(TAG, "onCreate: $it,${getSpanStart(it)},${getSpanEnd(it)}")
                }
            }
        }
        showMenu.setOnClickListener {
            stylemenu.visibility = View.VISIBLE
        }
        val spannableStringBuilder = SpannableStringBuilder("1234567\n\u200B\n8912345678\n9asdfg")
        spannableStringBuilder.setSpan(BulletSpan(50), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableStringBuilder.setSpan(BulletSpan(50), 8, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        test.setText(spannableStringBuilder)

        test.filters = arrayOf(object :InputFilter{
            override fun filter(
                source: CharSequence?,
                start: Int,
                end: Int,
                dest: Spanned?,
                dstart: Int,
                dend: Int
            ): CharSequence {
                return source?:""
            }
        })
        test.addSelectionListener { start, end ->
            test.text!!.getSpans<ParagraphStyle>(start,end).forEach {
                Log.i(TAG, "onCreate: $it,$start,$end")
                val char = test.text!![start]
                if (char=='\n'){
                    Log.i(TAG, "onCreate: nnnnn")
                }else if (char==com.hxx.widget.richtext.TextUtils.REPLACE_CHAR){
                    Log.i(TAG, "onCreate: 200B")
                }else{
                    Log.i(TAG, "onCreate: $char")
                }

            }
        }
    }

    val TAG = "文本编辑"
}